//
//  FQDLabOptionCell.m
//  FakeQDaily
//
//  Created by mewxiv on 9/18/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDLabOptionCell.h"
#import "FQDRestModel.h"

#import "Masonry.h"
#import "UIImageView+WebCache.h"

static NSString *const kCheckMultiImage = @"voteOptionCheckMulti";
static NSString *const kCheckedMultiImage = @"voteOptionCheckedMulti";
static NSString *const kCheckSingleImage = @"voteOptionCheckSingle";
static NSString *const kCheckedSingleImage = @"voteOptionCheckedSingle";

@interface FQDLabOptionCell ()

@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIImageView *descImageView;
@property (nonatomic, strong) UILabel *descLabel;
@property (nonatomic, strong) UIImageView *checkBoxImageView;

@end

@implementation FQDLabOptionCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
        [self prepareConstraints];
    }
    return self;
}

- (void)prepareUI
{
    _backView = [[UIView alloc] init];
    _backView.backgroundColor = RGB(237, 237, 237);
    [self.contentView addSubview:_backView];
    
    _descImageView = [[UIImageView alloc] init];
    _descImageView.contentMode = UIViewContentModeScaleAspectFill;
    [_backView addSubview:_descImageView];
    
    _descLabel = [[UILabel alloc] init];
    _descLabel.font = FQDRegularFontWithSize(14.0f);
    _descLabel.textColor = RGBA(42, 42, 42, 204);
    _descLabel.numberOfLines = 3;
    _descLabel.preferredMaxLayoutWidth = 307.0f;
    [_backView addSubview:_descLabel];
    
    NSString *checkImageName = (self.canChooseMulti) ? kCheckMultiImage : kCheckSingleImage;
    UIImage *checkImage = [UIImage imageNamed:checkImageName];
    _checkBoxImageView = [[UIImageView alloc] init];
    _checkBoxImageView.contentMode = UIViewContentModeScaleAspectFill;
    _checkBoxImageView.image = checkImage;
    [_backView addSubview:_checkBoxImageView];
    
}

- (void)prepareConstraints
{
    WeakSelf;
    
    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView);
        make.left.equalTo(weakSelf.contentView).offset(10);
        make.right.equalTo(weakSelf.contentView).offset(-10);
        make.height.equalTo(weakSelf.contentView);
    }];
    
    [_descImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.equalTo(weakSelf.backView);
        make.width.and.height.equalTo(weakSelf.backView.mas_height);
    }];
    
    [_descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.equalTo(weakSelf.descImageView);
        make.left.equalTo(weakSelf.descImageView.mas_right).offset(13);
        make.right.equalTo(weakSelf.checkBoxImageView.mas_left).offset(-10);;
    }];
    
    [_checkBoxImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.backView).offset(-15);
        make.centerY.equalTo(weakSelf.backView);
        make.size.mas_equalTo(@24);
    }];
}

- (void)updateCheckBoxState
{
    UIImage *image;
    if (self.isChoosed) {
        NSString *checkedImageName = (self.canChooseMulti) ? kCheckedMultiImage : kCheckedSingleImage;
        image = [UIImage imageNamed:checkedImageName];
    } else {
        NSString *checkImageName = (self.canChooseMulti) ? kCheckMultiImage : kCheckSingleImage;
        image = [UIImage imageNamed:checkImageName];
    }
    self.checkBoxImageView.image = image;
}

+ (NSString *)reuseId
{
    return @"FQDLabOptionCell";
}

#pragma mark - Getters and Setters
- (void)setOption:(FQDLabOpinion *)option
{
    _option = option;
    
    NSURL *imageURL = [NSURL URLWithString:option.image];
    [self.descImageView sd_setImageWithURL:imageURL];
    
    self.descLabel.text = option.content;
}

- (void)setIsChoosed:(BOOL)isChoosed
{
    _isChoosed = isChoosed;
    
    [self updateCheckBoxState];
}

@end
